package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.domain.SysUserRole;
import com.demo.service.SysUserRoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.mapper.SysUserMapper;
import com.demo.domain.SysUser;
import com.demo.service.SysUserService;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;


@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService{

    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 分页查询用户数据
     * 涉及表： sys_user
     * @param page
     * @param mobile
     * @param fullname
     * @return
     */
    @Override
    public Page<SysUser> findByPage(Page<SysUser> page, String mobile, String fullname) {
        return  page(page,
                    new LambdaQueryWrapper<SysUser>()
                                .like(!StringUtils.isEmpty(mobile),SysUser::getMobile,mobile)
                                .like(!StringUtils.isEmpty(fullname),SysUser::getFullname,fullname));
    }

    @Override
    @Transactional
    public boolean addUser(SysUser sysUser) {
        //获取用户 密码
        String password = sysUser.getPassword();
        //获取用户 角色
        String role_strings = sysUser.getRole_strings();
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        //对密码进行加密
        String encode = bCryptPasswordEncoder.encode(password);
        sysUser.setPassword(encode);
        boolean save = super.save(sysUser);
        if(save){
            //给用户新增权限数据
            if(!StringUtils.isEmpty(role_strings)){

                String[] roleIds = role_strings.split(",");
                ArrayList<SysUserRole> sysUserRoles = new ArrayList<>(roleIds.length);
                for (String roleId : roleIds) {
                    SysUserRole sysUserRole = new SysUserRole();
                    sysUserRole.setRoleId(Long.valueOf(roleId));
                    sysUserRole.setUserId(sysUser.getId());
                    sysUserRoles.add(sysUserRole);
                }
                boolean b = sysUserRoleService.saveBatch(sysUserRoles);
                return b;
            }
        }

        return save;
    }

    @Override
    public boolean updateUser(SysUser sysUser) {
        boolean update = super.updateById(sysUser);
        //1.获取用户角色
        String role_strings = sysUser.getRole_strings();
        
        //先删除用户角色
        boolean remove = sysUserRoleService.remove(new LambdaQueryWrapper<SysUserRole>().in(SysUserRole::getUserId, sysUser.getId()));
        //新增用户角色
        if(remove){

            //给用户新增权限数据
            if(!StringUtils.isEmpty(role_strings)){

                String[] roleIds = role_strings.split(",");
                ArrayList<SysUserRole> sysUserRoles = new ArrayList<>(roleIds.length);
                for (String roleId : roleIds) {
                    SysUserRole sysUserRole = new SysUserRole();
                    sysUserRole.setRoleId(Long.valueOf(roleId));
                    sysUserRole.setUserId(sysUser.getId());
                    sysUserRoles.add(sysUserRole);
                }
                boolean b = sysUserRoleService.saveBatch(sysUserRoles);
                return b;
            }
        }
        return update;
    }

    @Override
    @Transactional
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        boolean b = super.removeByIds(idList);
        if(b){
            boolean remove = sysUserRoleService.remove(new LambdaQueryWrapper<SysUserRole>().in(SysUserRole::getUserId, idList));
            return remove;
        }

        return b;
    }
}
